<?php

namespace common\models\member;

use Yii;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%member_cashout_order}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $member_id 会员ID
 * @property int $cashout_id
 * @property string $paymode 支付方式
 * @property string $order_no 订单号
 * @property int $amount 订单金额
 * @property int $recharge 手续费金额
 * @property int $pay 支付金额
 * @property int $pay_time 支付时间
 * @property string $pay_no 交易号
 * @property int $pay_status 佣金汇款状态
 * @property int $expired_at 未支付过期时间
 * @property string $description
 * @property string $mark 备注
 * @property int $status 订单状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberCashoutOrder extends \common\db\ActiveRecord {

    /**
     * 结算方式
     * 默认0，使用后2，购买后1
     */
    const PAY_STATUS_DEFAULT = 0;
    const PAY_STATUS_ACTIVE = 10;

    /**
     * @var const
     */
    const STATUS_CLOSE = -10; //已关闭
    const STATUS_CANCEL = -1; //已取消
    const STATUS_DEFAULT = 0; //默认
    const STATUS_ACTIVE = 10; //已提交，待付款
    const STATUS_COMPLETE = 100; //已评价,待交易关闭

    /**
     * 订单支付方式
     * @var PAYMODE_WECHAT  wechat  微信支付
     * @var PAYMODE_ALIPAY  alipay  支付宝支付
     * @var PAYMODE_APPLE   apple   苹果支付
     * @var PAYMODE_CREDIT  credit  积分支付
     */
    const PAYMODE_WECHAT = 'wechat';
    const PAYMODE_ALIPAY = 'alipay';
    const PAYMODE_CREDIT = 'credit';
    const PAYMODE_OFFLINE = 'offline';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_cashout_order}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'member_id', 'cashout_id', 'amount', 'recharge', 'pay', 'pay_time', 'pay_status', 'expired_at', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['order_no'], 'required'],
            [['paymode'], 'string', 'max' => 10],
            [['order_no', 'pay_no'], 'string', 'max' => 32],
            [['description'], 'string', 'max' => 64],
            [['mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'member_id' => '会员ID',
            'cashout_id' => 'Cashout ID',
            'paymode' => '支付方式',
            'order_no' => '订单号',
            'amount' => '订单金额',
            'recharge' => '优惠金额',
            'pay' => '支付金额',
            'pay_time' => '支付时间',
            'pay_no' => '交易号',
            'pay_status' => '佣金汇款状态',
            'expired_at' => '未支付过期时间',
            'description' => 'Description',
            'mark' => '备注',
            'status' => '订单状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getApply() {
        return $this->hasOne(MemberCashout::class, ['id' => 'cashout_id']);
    }

    public function getMember() {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    public function getAccount() {
        return $this->hasOne(MemberAccount::class, ['member_id' => 'member_id']);
    }

    public function getPaymodeNo() {
        return [
            static::PAYMODE_WECHAT => 4,
            static::PAYMODE_ALIPAY => 2,
            static::PAYMODE_CREDIT => 1,
            static::PAYMODE_OFFLINE => 0,
        ];
    }

    /**
     * 生成唯一交易号
     */
    public function generateNo() {
        $pcode = ArrayHelper::getValue($this->paymodeNo, $this->paymode, 0);
        $date = date('Ymd');
        $from = $this->user_id;
        $rules = [
            'paymode' => $pcode,
            'from' => str_pad(dechex($from), 5, "0", STR_PAD_LEFT),
            'date' => substr($date, 2),
        ];
        do {
            $micro = round((microtime(true) - strtotime($date)) * 10000);
            $rules['micro'] = str_pad($micro, 9, 0, STR_PAD_RIGHT);
            $tn = implode($rules);
            $record = static::find()->where(['order_no' => $tn])->exists();
        } while ($record);
        return $this->order_no = $tn;
    }

    public function getIsPaid() {
        return $this->pay_time > $this->created_at && $this->pay_status == static::PAY_STATUS_DEFAULT;
    }

    
}
